package com.hmxx.myblogproject.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hmxx.myblogproject.entity.MyBlog;
import com.hmxx.myblogproject.mapper.MyBlogBlogMapper;
import com.hmxx.myblogproject.mapper.MyBlogTitlesMapper;
import com.hmxx.myblogproject.service.MyBlogBlogService;
import com.hmxx.myblogproject.util.ResponseUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @description:
 * @Author: {27106}
 * @Date: {2021/11/22 9:22}
 */
@Service
public class MyBlogBlogServiceImpl implements MyBlogBlogService {
    @Resource
    private MyBlogBlogMapper mapper;

    @Resource
    private MyBlogTitlesMapper myBlogTitlesMapper;

    @Override
    public Object setBlog(String json) {
        JSONObject jsonObject= JSON.parseObject(json);
        String blog=jsonObject.getString("blog");
        String title_id=jsonObject.getString("title_id");
        MyBlog myBlog=new MyBlog();
        myBlog.setBlogBody(blog);
        int res=mapper.insertBlog(myBlog);
        if(res==0){
            return ResponseUtil.fail(2,"新增博客失败");
        }
        String blog_id=myBlog.getId();

        int r=myBlogTitlesMapper.updateTitleBlogId(title_id,blog_id);
        if(r>0){
            return ResponseUtil.ok();
        }else {
            return ResponseUtil.fail(1,"更新文章失败");
        }
    }

    @Override
    public Object updateBlog(String json) {
        JSONObject jsonObject= JSON.parseObject(json);
        String blog=jsonObject.getString("blog");
        String blog_id=jsonObject.getString("blog_id");
        MyBlog myBlog=new MyBlog();
        myBlog.setBlogBody(blog);
        myBlog.setId(blog_id);
        int res=mapper.updateBlog(myBlog);
        if(res>0){
            return ResponseUtil.ok();
        }else {
            return ResponseUtil.fail();
        }
    }
}
